﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using dasBlog.Storage;
using System.Collections.ObjectModel;
using newtelligence.DasBlog.Runtime.Configuration;
using System.Configuration;

namespace newtelligence.DasBlog.Runtime
{
    public class Runtime
    {
        public static void Start()
        {
            RuntimeSection runtimeConfig = ConfigurationManager.GetSection("dasBlog.Runtime") as RuntimeSection;
            if (runtimeConfig != null)
            {
                ProcessLoggingDataServiceConfig(runtimeConfig);
                ProcessBlogDataServiceConfig(runtimeConfig);
            }
        }

        private static void ProcessLoggingDataServiceConfig(RuntimeSection runtimeConfig)
        {
            if (runtimeConfig.LogDataService != null)
            {
                Type serviceType = Type.GetType(runtimeConfig.LogDataService.Type);
                if (serviceType != null)
                {
                    ILoggingDataService logDataService = Activator.CreateInstance(serviceType) as ILoggingDataService;
                    if (logDataService is ILoggingDataServiceInitialize)
                    {
                        ((ILoggingDataServiceInitialize)logDataService).Initialize(runtimeConfig.LogDataService.ContentLocation);
                    }
                    LoggingDataServiceFactory.SetDefaultService(logDataService);
                }
            }
        }

        private static void ProcessBlogDataServiceConfig(RuntimeSection runtimeConfig)
        {
            if (runtimeConfig.BlogDataService != null &&
                 runtimeConfig.BlogDataService.Type != null)
            {
                Type serviceType = Type.GetType(runtimeConfig.BlogDataService.Type);
                if (serviceType != null)
                {
                    IBlogDataService blogDataService = Activator.CreateInstance(serviceType) as IBlogDataService;
                    if (blogDataService is IBlogDataServiceInitialize)
                    {
                        ((IBlogDataServiceInitialize)blogDataService).Initialize(runtimeConfig.BlogDataService.ContentLocation, LoggingDataServiceFactory.GetService());
                    }
                    BlogDataServiceFactory.SetDefaultService(blogDataService);
                }
            }
        }

        public static void Stop()
        {
        }

    }
}
